package ims.servlet;

import ims.beans.ProductType;
import ims.constants.IMSConstants;
import ims.db.view.prodType.ViewProdTypesDAO;
import ims.db.view.prodType.ViewProdTypesDbRequest;
import ims.db.view.prodType.ViewProdTypesDbResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ProdTypeServlet
 */
@WebServlet("/ProdTypeServlet")
public class ProdTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProdTypeServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			response.setContentType("text/html;charset=UTF-8");
	        PrintWriter out = response.getWriter();
 
			//Retrieve all product types
			ViewProdTypesDbRequest dbRequest = new ViewProdTypesDbRequest();
			dbRequest.setViewAll(true);
			ViewProdTypesDAO db = new ViewProdTypesDAO();
			ViewProdTypesDbResponse dbResponse = (ViewProdTypesDbResponse) db.execute(dbRequest);
			ArrayList<ProductType>prodTypes = dbResponse.getProdTypes();
			
			
			//generating output of page
			out.println("<html>");
            out.println("<head>");
            out.println("<title>Product Types</title>");
            out.println("</head>");
            out.println("<a href='/IMS/product'>Product</a>&nbsp;&nbsp;");
            out.println("<a href='/IMS/searchCustOrder'>Search Customer Order</a>&nbsp;&nbsp;&nbsp;&nbsp;");
            out.println("<a href='/IMS/account.jsp'>Update Accounts</a>&nbsp;&nbsp;");
            out.println("<a href='/IMS/reports.jsp'>Reports</a></br></br>");
            out.println("<h1>Product Type</h1>");
            out.println("<body>");
            //add
            out.println("<form name='/IMS/addProdTypeForm' action='/IMS/addProdType' method='post'>" );
            out.println("Product Type: <input type='text' name='prodType' /> <input type='submit' value='Add' /> ");
            out.println(" </form> " );
            
            //delete
            out.println("<form name='deleteProdTypeForm' action='/IMS/deleteProdType' method='post'>" );
            out.println("Product Type: <select name='prodType'>");
            for(int i=0; i<prodTypes.size();i++)
            {
            	ProductType prodType = prodTypes.get(i);
            	out.println("<option value='" +prodType.getProdTypeId()+ ":"+ prodType.getProdType()+"'>" + prodType.getProdType()+"</option>" );
            			
            }
            out.println("</select>");
            out.println("<button type='submit' name='Delete' >Delete</button>");
            out.println(" </form> " );
            
            //update
            out.println("<form name='updateProdTypeForm' action='/IMS/prodType' method='post'>" );
            out.println("Product Type: <select name='prodType'>");
            for(int i=0; i<prodTypes.size();i++)
            {
            	ProductType prodType = prodTypes.get(i);
            	out.println("<option value='" +prodType.getProdTypeId()+ "'>" + prodType.getProdType()+"</option>" );
            			
            }
            out.println("</select>");
            out.println("<button type='submit' name='Update' >Update</button>");
            out.println(" </form> " );
            String respMsg = (String)request.getParameter(IMSConstants.RESPONSE_HEADER);
            if (respMsg!=null)
            	out.println(respMsg);
            out.println("</body>");

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			String prodTypeId = request.getParameter("prodType");
	
			//Retrieve product type
			ViewProdTypesDbRequest dbRequest = new ViewProdTypesDbRequest();
			dbRequest.setViewAll(false);
			dbRequest.setProdTypeId(prodTypeId);
			ViewProdTypesDAO db = new ViewProdTypesDAO();
			ViewProdTypesDbResponse dbResponse = (ViewProdTypesDbResponse) db.execute(dbRequest);
			ArrayList<ProductType>prodTypes = dbResponse.getProdTypes();
			
			response.setContentType("text/html;charset=UTF-8");
	        PrintWriter out = response.getWriter();
	        out.println("<html>");
            out.println("<head>");
            out.println("<title>Update Product Type Name</title>");
            out.println("</head>");
            out.println("<body>");
            StringBuffer form = new StringBuffer();
            form.append("<form name='/IMS/updateProdTypeForm' action='/IMS/updateProdType?prodTypeId=");
            form.append(prodTypeId).append("' method='post'>");
            //out.println("<form name='/IMS/updateProdTypeForm' action='/IMS/updateProdType?prodTypeId=' method='post'>" );
            out.println(form.toString());
            StringBuffer prod = new StringBuffer();
            prod.append("Please update the product type name: <input type='text' name='prodType' size=20 maxlength=20 ");
            prod.append("value='").append(prodTypes.get(0).getProdType()).append("' /><button type='submit' name='Update'>Update</button>");
            out.println(prod.toString());
            out.println(" </form> " );
            out.println("</body>");
            
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}
